package com.example.scanlogin;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class SHA512Util {

    public static String generateSHA512Hash(String input) {
        try {
            // 创建MessageDigest实例并指定SHA-512算法
            MessageDigest digest = MessageDigest.getInstance("SHA-512");
            // 将输入字符串转换为字节数组并生成哈希值
            byte[] hashBytes = digest.digest(input.getBytes());
            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString().toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            log.info("生成sha521失败!{}", e.getMessage());
            return null;
        }
    }
}
